package com.tal.bcc.pad.cloud.utils;

import com.tal.bcc.pad.cloud.entity.CloudDir;
import com.tal.bcc.pad.cloud.exception.CloudStorageException;
import com.tal.bcc.pad.cloud.utils.sign.binary.Hex;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class CloudUtil {
    private static final String signHeader = "TSS1-HMAC-SHA1";

    public CloudUtil() {
    }

    public static String getOssFileUri(String filePath, CloudDir cloudDir) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("/v3/").append("bcc-pad/").append(cloudDir.getName()).append("/");
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(1);
        int month = calendar.get(2) + 1;
        int date = calendar.get(5);
        stringBuilder.append(year + "-").append(String.format("%02d", month) + "-").append(String.format("%02d", date) + "-");
        UUID uuid = UUID.randomUUID();
        stringBuilder.append(uuid + "-");
        File file = new File(filePath);
        stringBuilder.append(file.getName());
        return stringBuilder.toString();
    }

    public static String getAuthorization(Map<String, String> headers, List<String> canonicalRequest) {
        String signature = getSignature(headers, canonicalRequest, "626ae643c9da9ccab6a6d117ab5e2f3a");
        String signedHeaders = "";
        Iterator<String> iterator = headers.keySet().iterator();

        while(iterator.hasNext()) {
            String key = (String)iterator.next();
            if ("".equals(signedHeaders)) {
                signedHeaders = key;
            } else {
                signedHeaders = signedHeaders + ';' + key;
            }
        }

        StringBuilder builder = (new StringBuilder()).append("TSS1-HMAC-SHA1").append(" AccessKeyId=").append("856ddde5f671ef66a3ba93df3c5a4d0a").append(",SignedHeaders=").append(signedHeaders).append(",Signature=").append(signature);
        String authorization = builder.toString();
        return authorization;
    }

    public static String getSignature(Map<String, String> headers, List<String> canonicalRequest, String accessKeySecret) {
        StringBuilder stringBuilder = new StringBuilder();

        for(int i = 0; i < canonicalRequest.size(); ++i) {
            if (i == 0) {
                stringBuilder.append((String)canonicalRequest.get(i));
            } else {
                stringBuilder.append("\n").append((String)canonicalRequest.get(i));
            }
        }

        String canonicalRequestString = stringBuilder.toString();
        String date = DateUtils.getCurrentTimeByCalendar();
        String time = headers.get("x-tss-date") == null ? date : (String)headers.get("x-tss-date");

        String stringToSign;
        String res;
        try {
            res = DateUtils.getUtc(time);
            stringToSign = "TSS1-HMAC-SHA1\n" + res + "\n" + canonicalRequestString + "\n";
        } catch (ParseException var10) {
            ParseException e = var10;
            throw new CloudStorageException("生成签名异常:" + e.getMessage());
        }

        try {
            res = hmacSha1(stringToSign, accessKeySecret);
            return res;
        } catch (Exception var9) {
            throw new CloudStorageException("生成签名异常:" + var9.getMessage());
        }
    }

    public static String hmacSha1(String data, String key) {
        String result = "";
        byte[] bytesKey = key.getBytes();
        SecretKeySpec secretKey = new SecretKeySpec(bytesKey, "HmacSHA1");

        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKey);
            byte[] macData = mac.doFinal(data.getBytes());
            byte[] hex = (new Hex()).encode(macData);
            result = new String(hex, "ISO-8859-1");
            return result;
        } catch (NoSuchAlgorithmException var8) {
            NoSuchAlgorithmException e = var8;
            e.printStackTrace();
            throw new CloudStorageException("NoSuchAlgorithmException:" + e.getMessage());
        } catch (InvalidKeyException var9) {
            InvalidKeyException e = var9;
            e.printStackTrace();
            throw new CloudStorageException("InvalidKeyException:" + e.getMessage());
        } catch (UnsupportedEncodingException var10) {
            UnsupportedEncodingException e = var10;
            e.printStackTrace();
            throw new CloudStorageException("UnsupportedEncodingException:" + e.getMessage());
        }
    }
}
